
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heuristics;

/**
 *
 * @author ahmad
 */
import java.awt.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.Arrays;

public class CentralizedGreedyCoverage extends CoverageHeuristic {
    
    public CentralizedGreedyCoverage(String file, int number_of_orientations) {
        super(file, number_of_orientations);
    }
    
    public CentralizedGreedyCoverage(ArrayList<Rectangle> obstacles, ArrayList<Point2D.Double> cameras, ArrayList<Point2D.Double> people, int number_of_orientations) {
        super(obstacles, cameras, people, number_of_orientations,false);
    }

    public int[] reassign() {
        computeSpotted();
        boolean[] used = new boolean[number_of_cameras];
        boolean[] covered = new boolean[number_of_men];
        assignment = new int[number_of_cameras];
        int greedyCoverage = 0;
        for (int i = 0; i < number_of_cameras; i++) {
            int bestCamInd = -1, bestOrient = -1, best = -1;
            for (int camInd = 0; camInd < number_of_cameras; camInd++) {
                if (used[camInd]) {
                    continue;
                }
                int bestCamOrient = -1, bestCam = -1;
                for (int orient = 0; orient < number_of_orientations; orient++) {
                    int orientCov = 0;
                    
                    for (int p = 0; p < number_of_men; p++) {
                        if (covered[p]) {
                            continue;
                        }
                        
                        if (spotted[camInd][0][orient][p]) {
                            orientCov++;
                        }
                    }
                    if (bestCam < orientCov) {
                        bestCam = orientCov;
                        bestCamOrient = orient;
                    }
                }
                if (best < bestCam) {
                    best = bestCam;
                    bestOrient = bestCamOrient;
                    bestCamInd = camInd;
                }
            }
            used[bestCamInd] = true;
            assignment[bestCamInd] = bestOrient;
            for (int p = 0; p < number_of_men; p++) {
                if (spotted[bestCamInd][0][bestOrient][p]) {
                    if (!covered[p]) {
                        greedyCoverage++;
                    }
                    covered[p] = true;
                }
            }
        }
        return assignment;
    }
    @Override
    public String toString() {
        return "Centralized Greedy";
    }
}